// 加载js
var loadScript = function (url) {
	var $dynamicScript = document.createElement('script');
	$dynamicScript.type = 'text/javascript';
	$dynamicScript.async = true;
	$dynamicScript.src = url;
	document.body.appendChild($dynamicScript);
};
// 加载css
var loadStyle = function (url) {
	var $dynamicStyle = document.createElement('link');
	$dynamicStyle.setAttribute('rel', 'stylesheet');
	$dynamicStyle.setAttribute('type', 'text/css');
	$dynamicStyle.setAttribute('href', url);
	document.head.appendChild($dynamicStyle);
};
// 加载meta
var loadMeta = function (nameKey, nameVal, content) {
	var $dynamicMeta = document.createElement('meta');
	$dynamicMeta.setAttribute(nameKey, nameVal);
	$dynamicMeta.setAttribute('content', content);
	document.head.appendChild($dynamicMeta);
};

var hasShareParam = !!(new RegExp('\\?(?:.+(?:&|=))?shared=([^&#]*)(?:&|#|$)').exec(location.search) || [])[1] || false;

var pre = function () {
	// APP中显示分享
	if (!hasShareParam) {
		try {
			AppBridge.shareData(JSON.stringify({
				'logoUrl': 'https://hh.ppdaicdn.com/static/images/home/logo.png?v=2',
				'title': '狂送现金红包，人人有份！',
				'subTitle': '庆上市大回馈！还卡超人用户独享福利！红包可立即提现！',
				'shareUrl': 'https://m.huankachaoren.com/national_day?shared=1',
				'redbag': 0
			}));
		} catch(ex) {
			console.log('需要在APP中使用');
		}
	}

	$('.main')
		.addClass('double-eleven')
		.empty()
		.append(
			$('<div class="section first">')
				.append(
					$('<img class="bkg bkg_01" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/bkg_01_01.png?v=1">')
				)
				.append(
					$('<img class="bkg money" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/money_01.png?v=2">')
				)
				.append(
					$('<img class="bkg pkg" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/package_01.png?v=2">')
				)
				.append(
					$('<img class="bkg btn" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/btn_01.png?v=1">')
				)
				.append(
					$('<img class="bkg hand" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/hand_01.png?v=1">')
				)
				.append(
					$('<img class="bkg bkg_02" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/bkg_01_02.png?v=1">')
				)
				.append(
					$('<a id="go">').on('click', function () {
						// 统计
						sa.track('h5_clk', {
							'tgt_type': 'button',
							'tgt_name': 'h5-ACT-还卡超人上市送红包-我要领红包',
							'tgt_event_id': 'h5_ACT_button',
							'target_url': '',
							'url': location.href
						});
						if (hasShareParam) {
							location.assign('//a.app.qq.com/o/simple.jsp?pkgname=com.ppdai.huanapp');
							return;
						}
						try {
							AppBridge.goLoan();
						} catch(ex) {
							location.assign('http://m.ppdai.com/?hh#1');
						}
					})
				)
		)
		.append(
			$('<div class="section second">')
				.append(
					$('<img class="bkg bkg_01" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/bkg_02_01.png?v=3">')
				)
				.append(
					$('<div class="process">')
						.append(
							$('<img class="bkg proc" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/bkg_02_02.png?v=4">')
						)
						.append(
							$('<a>').on('click', function () {
								$('.double-eleven-alert').addClass('show');
							})
						)
				)
				.append(
					$('<div class="marquee-container">')
						.append(
							$('<img class="bkg list" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/bkg_02_03.png?v=2">')
						)
						.append(
							$('<div class="marquee">')
								.append(
									$('<ul class="slider">')
								)
								.append(
									$('<ul class="slider">')
								)
						)
				)
		)
		.after(
			$('<div class="double-eleven-alert">')
				.append(
					$('<img class="bkg alert_bkg" src="//m.ppdaicdn.com/act/huan/img/shuangshiyi/alert_01.png?v=2">')
				)
				.append(
					$('<a>').on('click', function () {
						$('.double-eleven-alert').removeClass('show');
					})
				)
		)
		.hide();
};

var main = function () {
	// 定义变量
	var $slider = $('.slider').first();
	var ulOffsetTop = -300,
		ulHeight = 0,
		marqueeStep = 0.3;
	requestAnimationFrame = requestAnimationFrame ||
		mozRequestAnimationFrame ||
		webkitRequestAnimationFrame;
	var marquee = function () {
		// 获取当前滚动参数
		ulOffsetTop -= marqueeStep;
		if (ulOffsetTop + ulHeight < 0) {
			ulOffsetTop += ulHeight;
		}
		$slider.css('margin-top', ulOffsetTop + 'px');
		requestAnimationFrame(function () {
			marquee();
		});
	};

	// 显示页面
	$('.main')
		.show()
		.css('visibility', 'visible')
		.fullpage({
			'afterResize': function () {
				$('.main').fullpage.reBuild();
			}
		});

	// 请求接口
	$.ajax({
		'url': '//m.huankachaoren.com/api/activity/data.htm',
		'type': 'GET'
	}).done(function (data) {
		// 填充数据
		var $temp = $('<ul>');
		$.each(data.userList, function (index, item) {
			$temp.append($('<li>用户' + item.mobile + '参与活动，借款' + item.amount + '元！</li>'));
		});
		$('.slider').append($temp.children());

		// 获取初始滚动参数
		ulOffsetTop = $slider.get(0).offsetTop;
		ulHeight = $slider.get(0).offsetHeight;

		// 滚动
		marquee();
	});
};

// 加载meta
loadMeta('itemprop', 'image', 'https://hh.ppdaicdn.com/static/images/home/logo.png?v=2');
// 加载css
loadStyle('//m.ppdaicdn.com/act/huan/css/shuangshiyi/national_day.css?v=9');

(function hasJquery() {
	if (window.jQuery) {
		// 加载jQuery.easing插件
		loadScript('//m.ppdaicdn.com/act/huan/js/jquery.easing.min.js');

		// 预操作
		pre();

		(function hasEasing() {
			if ($.easing.jswing) {
				// 加载jQuery.fullPage插件
				loadScript('//m.ppdaicdn.com/act/huan/js/jquery.fullpage.min.js?v=1');

				(function hasFullpage() {
					if ($.fn.fullpage) {
						// 调用主流程
						main();

						return;
					}
					setTimeout(hasFullpage, 50);
				}());

				return;
			}
			setTimeout(hasEasing, 50);
		}());

		return;
	}
	setTimeout(hasJquery, 50);
}());
